package edu.hawaii.halealohacli;

import java.util.AbstractSet;
import java.util.TreeSet;

/**
 * A simple Collection to sort data using a TreeSort.
 * 
 * @author John MacMillan
 * @author Mitchell Kupfer
 * @author Bryson Hori
 * 
 */
public class Storage {

  AbstractSet<Nod> tree;

  /**
   * Default constructor, no parameters.
   */
  public Storage() {
    tree = new TreeSet<Nod>();
  }

  /**
   * Adds a node with the passed in name and number.
   * 
   * @param name name of the node.
   * @param number number stored in the node.
   */
  public void putInfo(String name, long number) {
    Nod nody = new Nod(name, number);
    tree.add(nody);
  }

  /**
   * Returns the first node in the treeSet and deletes it.
   * 
   * @return the first node in the treeSet
   */
  public AbstractSet<Nod> getInfo() {
    return tree;
  }

}

/**
 * Node class used within Storage class, holds a name and a number for sorting. implements
 * comparable.
 * 
 * @author mitchell kupfer
 * 
 */
class Nod implements Comparable<Nod> {
  String name;
  long number;

  /**
   * Default constructor takes a name and a number.
   * 
   * @param name the name of a given object.
   * @param number the number on which to be sorted.
   */
  public Nod(String name, long number) {
    this.name = name;
    this.number = number;
  }

  /**
   * An override for the compareTo method using the number field.
   * 
   * @return int used for default compareTo.
   * @param node the node that is being compared to.
   */
  @Override
  public int compareTo(Nod node) {

    if (node.number < this.number) {
      return 1;
    }

    // else if (node.number == this.number) {
    // return 0;
    // }

    else {
      return -1;
    }
  }

  /**
   * Override for equals method.
   * 
   * @return a boolean for equality.
   * @param node the node to be compared to.
   */
  @Override
  public boolean equals(Object node) {
    if (node instanceof Nod) {

      Nod nody = (Nod) node;
      if (this.number == nody.number && this.name.equals(nody.name)) {
        return true;
      }
    }
    return false;
  }

  /**
   * Override method for the hashcode.
   * 
   * @return the int hash code.
   */
  @Override
  public int hashCode() {
    return (int) this.number % 10;
  }
}
